<?php

namespace app\admin\controller\wechat;

use app\admin\controller\Base;
use think\facade\Request;
use think\facade\View;
use think\facade\Db;
use app\common\model\Wechat as WechatModel;


class Wechat extends Base{
    public function initialize()
    {
        parent::initialize(); 
    }

    public function config(){
        if (Request::isPost()) {
            $page = $this->request->post('page') ? $this->request->post('page') : 1;
            $limit = $this->request->post('limit') ? $this->request->post('limit') : 20;
            $where = []; 

            $list = WechatModel::loadList($where,$limit);
            
            return $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['list'], 'count' => $list['count']];
        }
        return view();

    }

    public function add(){
        if (Request::isPost()) {
            $data = $this->request->post();
            $data['appid'] = trim($data['appid']);
            $data['appsecret'] = trim($data['appsecret']);
            $data['mchid'] = trim($data['mchid']);
            $data['paykey'] = trim($data['paykey']);
            $res = WechatModel::find($data['appid']);
            if ($res) {
                $this->error('公众号已存在!');
            }
            $data['returnurl'] = str_replace('{$domain}',$data['domain'],$data['returnurl']);
            $res = WechatModel::create($data);
            if ($res) {
                $this->success(lang('add success'),url('config'));
            } else {
                $this->error(lang('add fail'));
            }
        }
        $call_back_url = '{$domain}/wechat/RelateServer/relateWeixin';
        $view = [
            'info' => '',
            'call_back_url' => $call_back_url,
        ];
        View::assign($view);
        return view();
    }

    public function edit(){
        if (Request::isPost()) {
            $data = $this->request->post();
            $data['appid'] = trim($data['appid']);
            $data['appsecret'] = trim($data['appsecret']);
            $data['mchid'] = trim($data['mchid']);
            $data['paykey'] = trim($data['paykey']);
            $res = WechatModel::find($data['appid']);
            if ($res) {
                $this->error('公众号已存在!');
            }
            $data['returnurl'] = str_replace('{$domain}',$data['domain'],$data['returnurl']);
            
            $res = WechatModel::where('id',$data['id'])->update($data);
            if ($res !== false) {
                $this->success(lang('edit success'), url('config'));
            } else {
                $this->error(lang('edit fail'));
            }
        }
        $info = WechatModel::find(Request::get('id'));
        $call_back_url = '{$domain}/wechat/RelateServer/relateWeixin';
        $view = [
            'info' => $info,
            'title' => '修改',
            'call_back_url' => $call_back_url,
        ];
        View::assign($view);
        return view('add');

    }
    public function state(){
        $data = $this->request->post();
        $id = $this->request->post('id');
        if($id){
            $info = WechatModel::find($id);
            $field = $data['field'];
            $status = $info->$field==1?0:1;
            WechatModel::where('id',$id)->update(['status'=>$status]);

            $this->success(lang('edit success'));

        }else{
            $this->error(lang('invalid data'));
        }
    }
    public function del(){
        $ids = $this->request->post('ids');
        if($ids){
            WechatModel::where('id','in',$ids)->delete();
            $this->success(lang('delete success'));
        }else{
            $this->error(lang('invalid data'));
        }
    }
    
}